package com.liwenfeng.tcpandupd;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UpdServer {
    public static void main(String[] args){
        System.out.println("服务端启动了。。。。。。");
        //创建一个服务端对象，注册端口
        try(DatagramSocket socket = new DatagramSocket(8888);) {
            //数据包最大容量是64KB
            byte[] buffer = new byte[1024*64];
            //创建一个数据包对象，用于接收数据
            DatagramPacket packet = new DatagramPacket(buffer,buffer.length);

            while (true){
                //开始使用数据包接收数据，启动后会阻塞在这等待数据
                socket.receive(packet);
                //获取接收到的数据长度
                int len = packet.getLength();
                String rs = new String(buffer, 0, len);
                System.out.println(rs);
                if (rs.equals("quit")) break;
//                System.out.println(packet.getAddress().getHostAddress());
//                System.out.println(packet.getPort());
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
